{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 遍历列表\n",
    "\n",
    "- 指将列表中所有元素取出来"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过 `while` 循环遍历列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "\n",
    "i = 0\n",
    "while i < len(my_city):\n",
    "    print(my_city[i])\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过 `for` 循环遍历列表\n",
    "\n",
    "- 语法：\n",
    "    ```\n",
    "    for 变量 in 序列:\n",
    "        代码块\n",
    "    ```\n",
    "- `for` 循环的代码块会执行多次，序列中有几个元素就会执行几次\n",
    "    - 每执行一次，就会将序列中的一个元素赋值给变量\n",
    "    - 所以我们可以通过变量，来获取列表中的元素\n",
    "- `for` 循环除了创建方式以外，其余的都和 `while` 循环一样：\n",
    "    - 包括 `else`、`break`、`continue` 都可以在 `for` 循环中使用\n",
    "    - 而且 `for` 循环使用也更加简单\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cities = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "\n",
    "for city in cities:\n",
    "    print(city)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.4 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}